//
//  FXSlider.swift
//  ijkPlayerManager_Example
//
//  Created by JY_NEW on 2021/11/30.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import UIKit

class FXSlider: UISlider {
    var lastBounds: CGRect = .zero
    var thumbBoundX: CGFloat = 10
    var thumbBoundY: CGFloat = 20
    override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect {
        let result = super.thumbRect(forBounds: bounds, trackRect: rect, value: value)
        lastBounds = result
        return result
    }
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let result = super.hitTest(point, with: event)
        if point.x < 0 || point.x > self.bounds.size.width {
            return result
        }
        
        if (point.y >= -self.thumbBoundY) && (point.y < lastBounds.size.height + self.thumbBoundY) {
            var value: Float = 0
            value = Float(point.x - self.bounds.origin.x)
            value = value / Float(self.bounds.size.width)
            
            value = value < 0 ? 0 : value
            value = value > 1 ? 1 : value
            
            value = value * (self.maximumValue - self.minimumValue) + self.minimumValue
            self.setValue(value, animated: true)
        }
        return result
    }
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        var result = super.point(inside: point, with: event)
        if !result && point.y > -10 {
            if (point.x >= lastBounds.origin.x - thumbBoundX) && (point.x <= (lastBounds.origin.x + lastBounds.size.width + thumbBoundX)) && (point.y < (lastBounds.size.height + thumbBoundY)) {
                result = true
            }
            
        }
        return result
    }
}
